package ca.camosun.inventory;

import ca.camosun.inventory.database.BoxAccessor;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

	public class SearchViewActivity extends Activity implements SearchView.OnQueryTextListener,
	SearchView.OnCloseListener {
	 
	    private ListView mListView;
	    private SearchView searchView;
	    private BoxAccessor mDbHelper;
	 
	    private TextView boxText;
	    private TextView itemNameText;
	    private TextView itemDescText;
	    private TextView itemQuantityText;
	 
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	 
	        searchView = (SearchView) findViewById(R.id.action_search);
	        searchView.setIconifiedByDefault(false);
	        searchView.setOnQueryTextListener(this);
	        searchView.setOnCloseListener(this);
	 
	        mListView = (ListView) findViewById(R.id.listName);
	 
	        mDbHelper = new BoxAccessor(this);
	        mDbHelper.open();
	 
	    }
	 
	    @Override
	    protected void onDestroy() {
	        super.onDestroy();
	        if (mDbHelper  != null) {
	            mDbHelper.close();
	        }
	    }
	 
	    public boolean onQueryTextChange(String newText) {
	        showResults(newText + "*");
	        return false;
	    }
	 
	    public boolean onQueryTextSubmit(String query) {
	        showResults(query + "*");
	        return false;
	    }
	 
	    public boolean onClose() {
	        showResults("");
	        return false;
	    }
	 
	    private void showResults(String query) {
	    	Cursor cursor = mDbHelper.searchItem(query);
	    	
	        if (cursor == null) {
	            
	        } else {
	            // Specify the columns we want to display in the result
	            String[] from = new String[] {
	            		BoxAccessor.DatabaseHelper.BOX_NAME,
	            		BoxAccessor.DatabaseHelper.ITEM_NAME,
	            		BoxAccessor.DatabaseHelper.ITEM_DESC,
	            		BoxAccessor.DatabaseHelper.ITEM_QUANTITY
	            		};
	 
	            // Specify the Corresponding layout elements where we want the columns to go
	            int[] to = new int[] {     
	            		R.id.sboxName,
	            		R.id.sitemName,
	            		R.id.sitemDesc,
	            		R.id.sitemQuantity};
	 
	         // Create a simple cursor adapter for the definitions and apply them to the ListView
	            SimpleCursorAdapter customers = new SimpleCursorAdapter(this,R.layout.item_result, cursor, from, to);
	            mListView.setAdapter(customers);
	            
	            // Define the on-click listener for the list items
	            mListView.setOnItemClickListener(new OnItemClickListener() {
	                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	                    // Get the cursor, positioned to the corresponding row in the result set
	                    Cursor cursor = (Cursor) mListView.getItemAtPosition(position);

	                    String box = cursor.getString(cursor.getColumnIndexOrThrow("box"));
	                    String itemName = cursor.getString(cursor.getColumnIndexOrThrow("item_name"));
	                    String itemDesc = cursor.getString(cursor.getColumnIndexOrThrow("item_desc"));
	                    String itemQuantity = cursor.getString(cursor.getColumnIndexOrThrow("item_quantity"));
	  
	                    boxText = (TextView) findViewById(R.id.sboxName);
	                    itemNameText = (TextView) findViewById(R.id.sitemName);
	                    itemDescText = (TextView) findViewById(R.id.sitemDesc);
	                    itemQuantityText = (TextView) findViewById(R.id.sitemQuantity);
	                    
	                    boxText.setText(box);
	                    itemNameText.setText(itemName);
	                    itemDescText.setText(itemDesc);
	                    itemQuantityText.setText(itemQuantity);
	            
	                    searchView.setQuery("",true);
	                }
	            });
	        }
	    }      
}
